package net.tv90.bfmccore.game;

import net.tv90.bfmccore.game.impl.EnumSpawnPolicy;
import net.tv90.bfmccore.helper.FactionHelper;
import net.tv90.bfmccore.util.GeneralUtil;
import net.tv90.bfmccore.util.vector.Vector3i;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;

import java.util.List;

import static net.tv90.bfmccore.slot.SlotHelper.giveSlot;

public class GameUtils {
    /**
     * 设置玩家重生点坐标
     *
     * @param p   玩家
     * @param vec 三维坐标
     */
    public static void setSpawnPoint(Player p, Vector3i vec) {
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "spawnpoint " + p.getName() + " " + vec.x + " " + vec.y + " " + vec.z);
    }

    /**
     * 复活逻辑
     *
     * @param p         玩家
     * @param pointList 复活点List
     * @param policy    复活逻辑
     * @return 复活坐标
     */
    public static Vector3i getExactSpawnPoint(Player p, List<Vector3i> pointList, EnumSpawnPolicy policy) {
        return getExactSpawnPoint(p, pointList, policy, 4);
    }

    /**
     * 复活逻辑
     *
     * @param p         玩家
     * @param pointList 复活点List
     * @param policy    复活逻辑
     * @return 复活坐标
     */
    public static Vector3i getExactSpawnPoint(Player p, List<Vector3i> pointList, EnumSpawnPolicy policy, int maxCheckNum) {
        switch (policy) {
            case RANDOM: {
                return GeneralUtil.getRandomElement(pointList);
            }
            //随机取出不大于maxCheckNum个传送点，比较最近敌人的距离，选择距离最大的复活
            case FURTHEST_TO_ENEMY: {
                int num = maxCheckNum;
                if (pointList.size() < num) {
                    num = pointList.size() <= 1 ? 1 : pointList.size() - 1;
                }
                List<Vector3i> sublist = GeneralUtil.getRandomSublist(pointList, num);
                List<Player> enemies = FactionHelper.getEnemies(p);
                Vector3i result = sublist.get(0);
                double nearestDistance = -1;
                for (Vector3i point : sublist) {
                    Location location = new Location(p.getWorld(), point.x, point.y, point.z);
                    double distance = FactionHelper.getNearestEnemyDistance(p, location, enemies);
                    if (distance > nearestDistance) {
                        nearestDistance = distance;
                        result = point;
                    }
                }
                return result;
            }
            case CLOSEST_TO_TEAMMATE: {
                int num = maxCheckNum;
                if (pointList.size() < num) {
                    num = pointList.size() <= 1 ? 1 : pointList.size() - 1;
                }
                List<Vector3i> sublist = GeneralUtil.getRandomSublist(pointList, num);
                List<Player> teammates = FactionHelper.getTeammates(p);  // Assuming there's a method to get teammates
                Vector3i result = sublist.get(0);
                double nearestDistance = Double.MAX_VALUE;
                for (Vector3i point : sublist) {
                    Location location = new Location(p.getWorld(), point.x, point.y, point.z);
                    double distance = FactionHelper.getNearestTeammateDistance(p, location, teammates);  // Assuming a method to get distance to nearest teammate
                    if (distance < nearestDistance) {
                        nearestDistance = distance;
                        result = point;
                    }
                }
                return result;
            }
        }
        throw new RuntimeException();
    }

    public static void vehicleDeploy(Player p) {
        p.playSound(p.getLocation(), "custom.Deploy", 20.0F, 1.0F);
        GameManager.inBattleFieldPlayers.add(p);
        try {
            giveSlot(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
